//
//  ViewControllerExtesion.swift
//  SwiftLearning
//
//  Created by 黄龙 on 2024/1/31.
//

import UIKit

extension UIViewController{
//MARK: - 在view上显示系统对话框
    func alertMessage(_ message: String,title: String = "",oktitle:String="知道了") {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        vc.addAction(UIAlertAction(title: oktitle, style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }
//默认左右2个按钮的弹窗
    func alertSheetMessage(_ message: String,title: String, okTitle:String="确定",cancelTitle:String="取消",iRed:Int?=0,okBlock:os_block_t?=nil,cancelBlock:os_block_t?=nil) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        vc.addAction(UIAlertAction(title: okTitle, style: (1 == iRed) ?.destructive : .default, handler: { action in
           okBlock?()
        }))
        vc.addAction(UIAlertAction(title: cancelTitle, style: (2 == iRed) ?.destructive : .default, handler: { action in
            cancelBlock?()
        }))
        present(vc, animated: true, completion: nil)
    }
    
//3个按钮(2个功能按钮+1个取消按钮)的系统对话框(类似：删除App/从屏幕移除/好的,3个按钮的系统弹窗会自动排成三行按钮样式)
    func alertMessage(_ message: String,andTitle title: String="", okTitle:String="确定",midTitle:String="再想想",cancelTitle:String? = "取消",iRed:Int?=0,okBlock:os_block_t?=nil,midBlock:os_block_t?=nil) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        vc.addAction(UIAlertAction(title: okTitle, style: (1 == iRed) ?.destructive : .default, handler: { OKAction in
           okBlock?()
        }))
        vc.addAction(UIAlertAction(title: midTitle, style: (2 == iRed) ?.destructive : .default, handler: { OKAction in
            midBlock?()
        }))
        vc.addAction(UIAlertAction(title: cancelTitle, style: .cancel, handler: nil))
        present(vc, animated: true, completion: nil)
    }
    
//3个按钮(2个自定义按钮+1个默认"取消"按钮)的sheet弹窗
        func alertSheetMessage(_ message: String,title: String, selection_1:String,selection_2:String,iRed:Int?=0,okBlock:os_block_t?=nil,midBlock:os_block_t?=nil) {
            let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)
            vc.addAction(UIAlertAction(title: selection_1, style: (1 == iRed) ?.destructive : .default, handler: { action in
               okBlock?()
            }))
            vc.addAction(UIAlertAction(title: selection_2, style: (2 == iRed) ?.destructive : .default, handler: { action in
                midBlock?()
            }))
            vc.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) //须有个cancel类型的按钮，才会点击空白关闭对话框
            present(vc, animated: true, completion: nil)
        }

//多个按钮的sheet弹窗
    func alertSheetMessage(_ message: String,title: String="", selections:[String],iRed:Int?=0,tapBlock:os_block_int?=nil) {
        let vc = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)
        for i in 0 ..< selections.count{
            let perTitle = selections[i]
            vc.addAction(UIAlertAction(title: perTitle, style: (iRed == i) ?.destructive : .default, handler: { action in
               tapBlock?(i)
            }))
        }
        vc.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) //须有个cancel类型的按钮，才会点击空白关闭对话框
        present(vc, animated: true, completion: nil)
    }
    
}
